﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Manage
{
    public partial class VisitorStatistics : BasePage_BackEnd
    {
        private static RequestGroupType groupType = RequestGroupType.Request_VisitorType;
        private int Sum = 0;
        public class RequestGroupModel
        {
            public string RequestTypeName { get; set; }
            public int RequestCount { get; set; }
            public float RequestPercent { get; set; }
        }

        protected void grRuquestReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var requestGroupModel = e.Row.DataItem as RequestGroupModel;
                var panProbress = e.Row.FindControl("panProbress") as Panel;
                panProbress.Width = new Unit(requestGroupModel.RequestPercent * 100, UnitType.Percentage);
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = Resources.ManageResource.mgr_lblTotalVisitCount;
                e.Row.Cells[1].Text = Sum.ToString();
            }
        }

        public override void InitControlResource()
        {
            lbtnVisitorType.Text = Resources.ManageResource.mgr_lblVisitorsClassification;
            lbtnLocation.Text = Resources.ManageResource.mgr_lblLocationName;
            lbtnLab.Text = Resources.ManageResource.mgr_lblLabName;
            lbtnProduct.Text = Resources.ManageResource.mgr_lblProductName;
            btnSelect.Text = Resources.ManageResource.mgr_btnQuery;
            int i = 1;
            //grRuquestReport.Columns[i++].HeaderText = Resources.ManageResource.mgr_lblVisitorsClassification;
            grRuquestReport.Columns[i++].HeaderText = Resources.ManageResource.mgr_lblVisitCount;
            grRuquestReport.Columns[i++].HeaderText = Resources.ManageResource.mgr_lblPercent;

            RuquestReportBind(groupType);
        }

        protected void lbtnVisitorType_Click(object sender, EventArgs e)
        {
            groupType = RequestGroupType.Request_VisitorType;
            RuquestReportBind(groupType);
        }

        protected void lbtnLocation_Click(object sender, EventArgs e)
        {
            groupType = RequestGroupType.Request_Location;
            RuquestReportBind(groupType);
        }

        protected void lbtnLab_Click(object sender, EventArgs e)
        {
            groupType = RequestGroupType.Request_Lab;
            RuquestReportBind(groupType);
        }

        protected void lbtnProduct_Click(object sender, EventArgs e)
        {
            groupType = RequestGroupType.Request_Product;
            RuquestReportBind(groupType);
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            RuquestReportBind(groupType);
        }

        private void RuquestReportBind(RequestGroupType eRequestGroupType)
        {
            lblOverError.Text = string.Empty;
            if (string.IsNullOrWhiteSpace(txtStartDate.Text) == false && string.IsNullOrWhiteSpace(txtEndDate.Text) == false)
            {
                if (string.Compare(txtStartDate.Text, txtEndDate.Text, true) > 0)
                {
                    lblOverError.Text = Resources.NameResource.OverTimeError + "！";
                    return;
                }
            }

            var listRequestGroupModel = new List<RequestGroupModel>();

            Expression<Func<Model.Request, bool>> searchExp = r => r.Status == 7;
            if (string.IsNullOrWhiteSpace(txtStartDate.Text) == false)
            {
                try
                {
                    var startDate = DateTime.Parse(txtStartDate.Text);
                    searchExp = searchExp.AndPredicate<Model.Request>(r => r.RequestDate >= startDate);
                }
                catch { }
            }

            if (string.IsNullOrWhiteSpace(txtEndDate.Text) == false)
            {
                try
                {
                    var endDate = DateTime.Parse(txtEndDate.Text);
                    searchExp = searchExp.AndPredicate(r => r.RequestDate <= endDate);
                }
                catch { }
            }

            var listRequest = RequestPresent.GetList(searchExp);

            switch (eRequestGroupType)
            {
                case RequestGroupType.Request_Location:
                    grRuquestReport.Columns[0].HeaderText = Resources.ManageResource.mgr_lblLocationName;
                    listRequestGroupModel = GetDataByLocation(listRequest);
                    break;
                case RequestGroupType.Request_Lab:
                    grRuquestReport.Columns[0].HeaderText = Resources.ManageResource.mgr_lblLabName;
                    listRequestGroupModel = GetDataByLab(listRequest);
                    break;
                case RequestGroupType.Request_Product:
                    grRuquestReport.Columns[0].HeaderText = Resources.ManageResource.mgr_lblProductName;
                    listRequestGroupModel = GetDataByProduct(listRequest);
                    break;
                case RequestGroupType.Request_VisitorType:
                default:
                    grRuquestReport.Columns[0].HeaderText = Resources.ManageResource.mgr_lblVisitorsClassification;
                    listRequestGroupModel = GetDataByVisitorType(listRequest);
                    break;
            }
            grRuquestReport.DataSource = listRequestGroupModel;
            grRuquestReport.DataBind();
        }

        private List<RequestGroupModel> GetDataByLocation(List<Model.Request> listRequest)
        {
            var listRequestGroupModel = new List<RequestGroupModel>();
            var lab = LabPresent.GetList();
            var location = LocationPresent.GetList();
            var group = from r in listRequest
                        join l in lab on r.LabId equals l.LabId
                        group l by l.LocationId into g
                        select new { g.Key, count = g.Count() };
            Sum = group.Sum(p => p.count);

            foreach (var type in location)
            {
                var first = group.FirstOrDefault(g => g.Key == type.LocationId);
                var count = first == null ? 0 : first.count;
                listRequestGroupModel.Add(new RequestGroupModel
                {
                    RequestTypeName = Language == Utility.Language.CN ? type.Name_CN : type.Name_EN,
                    RequestCount = count,
                    RequestPercent = count == 0 ? 0 : (float)count / Sum
                });
            }

            return listRequestGroupModel;
        }

        private List<RequestGroupModel> GetDataByLab(List<Model.Request> listRequest)
        {
            var listRequestGroupModel = new List<RequestGroupModel>();
            var lab = LabPresent.GetList();
            var group = from r in listRequest
                        group r by r.LabId into g
                        select new { g.Key, count = g.Count() };
            Sum = group.Sum(p => p.count);

            foreach (var type in lab)
            {
                var first = group.FirstOrDefault(g => g.Key == type.LabId);
                var count = first == null ? 0 : first.count;
                listRequestGroupModel.Add(new RequestGroupModel
                {
                    RequestTypeName = type.Name,
                    RequestCount = count,
                    RequestPercent = count == 0 ? 0 : (float)count / Sum
                });
            }

            return listRequestGroupModel;
        }

        private List<RequestGroupModel> GetDataByProduct(List<Model.Request> listRequest)
        {
            var listRequestGroupModel = new List<RequestGroupModel>();
            var visitTequest = VisitRequestPresent.GetList();
            var visitProduct = VisitProductPresent.GetList();
            var product = ProductPresent.GetList();
            var group = from r in listRequest
                        join vt in visitTequest on r.RequestId equals vt.RequestId
                        join vp in visitProduct on vt.VisitRequestId equals vp.VisitRequestId
                        group vp by vp.ProductId into g
                        select new { g.Key, count = g.Count() };
            Sum = group.Sum(p => p.count);

            foreach (var type in product)
            {
                var first = group.FirstOrDefault(g => g.Key == type.ProductId);
                var count = first == null ? 0 : first.count;
                listRequestGroupModel.Add(new RequestGroupModel
                {
                    RequestTypeName = Language == Utility.Language.CN ? type.Name_CN : type.Name_EN,
                    RequestCount = count,
                    RequestPercent = count == 0 ? 0 : (float)count / Sum
                });
            }

            return listRequestGroupModel;
        }

        private List<RequestGroupModel> GetDataByVisitorType(List<Model.Request> listRequest)
        {
            var listRequestGroupModel = new List<RequestGroupModel>();
            var visitRequest = VisitRequestPresent.GetList();
            var requestType = VisitTypePresent.GetList(v => v.Level != 1);
            var group = from r in listRequest
                        join v in visitRequest on r.RequestId equals v.RequestId
                        join vt in requestType on v.VisitType1 equals vt.Id
                        group v by v.VisitType1 into g
                        select new { g.Key, count = g.Count() };
            Sum = group.Sum(p => p.count);

            foreach (var type in requestType)
            {
                var first = group.FirstOrDefault(g => g.Key == type.Id);
                var count = first == null ? 0 : first.count;
                listRequestGroupModel.Add(new RequestGroupModel
                {
                    RequestTypeName = Language == Utility.Language.CN ? type.Name_CN : type.Name_EN,
                    RequestCount = count,
                    RequestPercent = count == 0 ? 0 : (float)count / Sum
                });
            }
            return listRequestGroupModel;
        }
    }

    public enum RequestGroupType
    {
        Request_VisitorType = 0,
        Request_Location = 1,
        Request_Lab = 2,
        Request_Product = 3,
    }
}